package org.dandsoft.blackbox.laboratory.samples.lighter;

/**
 * Created by IntelliJ IDEA.
 * User: dvilyuzhanin
 * Date: 24.03.2009 1:24:07
 */
public class LightSensor {
    public final static int TOP_TYPE = 0;
    public final static int RIGHT_TYPE = 1;
    public final static int BOTTOM_TYPE = 2;
    public final static int LEFT_TYPE = 3;

    public final static int NO_SUN = 0;
    public final static int SUN_THERE = 1;
    public final static int SUN = 2;

    private Lighter lighter;
    private int type;

    public LightSensor(int type, Lighter lighter) {
        this.lighter = lighter;
        this.type = type;
    }

    public int getState() {
        Sun sun = lighter.getWorld().getSun();

        double x = sun.getX() - lighter.getX();
        x = x * x;
        double y = sun.getY() - lighter.getY();
        y = y * y;
        double distance = Math.sqrt(x + y);

        if (TOP_TYPE == type) {
            if (sun.getY() < lighter.getY()) {
                if (distance > sun.getRange()) {
                    return SUN_THERE;
                } else {
                    return SUN;
                }
            } else {
                return NO_SUN;
            }
        } else if (RIGHT_TYPE == type) {
            if (sun.getX() > lighter.getX()) {
                if (distance > sun.getRange()) {
                    return SUN_THERE;
                } else {
                    return SUN;
                }
            } else {
                return NO_SUN;
            }
        } else if (BOTTOM_TYPE == type) {
            if (sun.getY() > lighter.getY()) {
                if (distance > sun.getRange()) {
                    return SUN_THERE;
                } else {
                    return SUN;
                }
            } else {
                return NO_SUN;
            }
        } else if (LEFT_TYPE == type) {
            if (sun.getX() < lighter.getX()) {
                if (distance > sun.getRange()) {
                    return SUN_THERE;
                } else {
                    return SUN;
                }
            } else {
                return NO_SUN;
            }
        }

        return NO_SUN;
    }


}
